package com.cjh.admin.controller;

import com.cjh.admin.config.TokenThreadLocal;
import com.cjh.common.dto.PermissionDTO;
import com.cjh.common.entity.Permission;
import com.cjh.common.response.Result;
import com.cjh.common.service.PermissionService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjh
 * @since 2021-11-15
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    PermissionService permissionService;

    @ApiOperation("获取所有菜单")
    @GetMapping("/getAllMenu")
    public Result getAllMenu() {
        return new Result().setCode(200).setMessage("操作成功").setData(permissionService.getMenuTree(0));
    }

    @ApiOperation("获取用户拥有的菜单")
    @GetMapping("/getMenuByUserId")
    public Result getMenuByUserId(@RequestParam Integer id,@RequestParam Integer permissionId) {
        if(permissionService.isSuperAdministrator(TokenThreadLocal.get())){
            return getAllMenu();
        }
        return  new Result().setCode(200).setMessage("操作成功").setData(permissionService.getMenuByUserId(id,permissionId));
    }

    @ApiOperation("获取一条菜单")
    @GetMapping("/getMenuById")
    public Result getMenuById(@RequestParam Integer id) {
        return new Result().setCode(200).setMessage("操作成功").setData(permissionService.getMenuById(id));
    }

    @ApiOperation("获取菜单的父菜单的idList")
    @GetMapping("/getFaththIdListByid")
    public Result getFaththIdListByid(@RequestParam Integer id) {
        return new Result().setCode(200).setMessage("操作成功").setData(permissionService.getFaththIdListByid(id));
    }
    @RequiresPermissions(value = {"addMenu"})
    @ApiOperation("新增菜单")
    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Permission permission) {
        return new Result().setCode(200).setMessage("操作成功").setData(permissionService.addmenu(permission));
    }
    @RequiresPermissions(value = {"updateMenu"})
    @ApiOperation("修改菜单")
    @PostMapping("/updateMenu")
    public Result updateMenu(@RequestBody Permission permission) {
        if(permission.getPermissionId().equals(permission.getFatherId())){
            return new Result().setCode(201).setMessage("父菜单不能为自身！");
        }
        List<PermissionDTO> allMenu = permissionService.getMenuTree(permission.getPermissionId());
        if(idIsInPermisionlist(allMenu,permission.getFatherId())){
            return new Result().setCode(201).setMessage("父菜单不能为当前菜单的子菜单！");
        }
        Integer integer = permissionService.updateMenu(permission);
        return new Result().setCode(200).setMessage("操作成功").setData(integer);
    }
    public Boolean idIsInPermisionlist(List<PermissionDTO> list,Integer s){
        for(PermissionDTO p:list){
            if(p.getPermissionId().equals(s)){
                return true;
            }
            if(p.getChildren().size() != 0){
                idIsInPermisionlist(p.getChildren(),s);
            }
        }
        return false;
    }
    @RequiresPermissions(value = {"deleteMenu"})
    @ApiOperation("删除菜单")
    @GetMapping("/deleteMenuById")
    public Result deleteMenuById(@RequestParam Integer id) {
        Integer integer = permissionService.deleteMenuById(id);
        return new Result().setCode(200).setMessage("操作成功").setData(integer);
    }

    @RequiresPermissions(value = {"deleteMenu"})
    @ApiOperation("删除多条菜单")
    @GetMapping("/deleteMenuByIdList")
    public Result deleteMenuByIdList(@RequestParam List<Integer> id) {
        Integer integer = permissionService.deleteMenuByIdList(id);
        return new Result().setCode(200).setMessage("操作成功").setData(integer);
    }
}
